/**
 *
 *  @auth xlx_good@qq.com
 *  @date 17/6/21.
 *
 */

requirejs.config({
    baseUrl: '../../',
    paths:  {
        avalon: 'javascript/avalon.min',
        jquery: 'js/plugins/jquery/jquery-2.1.1.min',
        pagination: 'lib/pagination/jquery.pagination',
        WdatePicker: 'js/plugins/DatePicker/WdatePicker',
        config: 'javascript/config',
        api: 'javascript/api',
        layer: 'js/plugins/layer1/layer',
        arg: 'javascript/arg'
    },
    shim: {
        pagination: {
            deps: ['jquery']
        },
        api: {
            deps: ['jquery']
        },
        layer: {
            deps: ['jquery']
        }
    }
});


(function(win, dom, requirejs, initVm){
    var vm;
    requirejs(['avalon', 'jquery', 'pagination', 'WdatePicker', 'config', 'api', 'layer','arg'], function(){
        //初始化vm
        vm = initVm(dom);
    });

})(window, document, requirejs, function(dom){
    var vm = avalon.define({
        $id: 'container',
        form: {
            startOrderTime: '',
            endOrderTime: '',
            code: '',
            buyerName: '',
			statusOrder: '',
			pageNum: 1,
			pageSize: 10
        },
		statusList: orderSubStatusList,
		keyword: '',
		keywordList: {
			"code": "子订单号",
			"buyerName": "采购商名称"
		},
		list: [],
        search: function() {
			for(var key in vm.keywordList) {
				vm.form[key] = null;
			}
			vm.form[$('#selKeyword').val()] = vm.keyword;
			Api.orderPage(vm.form, function(resp) {
				if(resp.code == 0) {
					if(vm.form.pageNum == 1) {
						setPagination(resp.data.total, resp.data.pageNum - 1, vm);
					}
					vm.list = resp.data.list;
				} else {
					layer.msg(resp.msg);
				}
			});
        },

    })
	avalon.filters.filterStatus = function(status) {
		return orderSubStatusMap[status];
	}

	avalon.filters.filterOption = function(el){
        var params = {
            code: el.code
        }
        return '<a href="'+Arg.url("./detail.html", params)+'">详情</a>';
    }

    avalon.scan(dom.body);
	vm.search();
    return vm;
});

/**
 * 设置翻页
 * @param <number> 总条数
 * @param <number> 当前页码
 * */
function setPagination(total, current, vm){
    $('.pagination').pagination(total,{
        items_per_page: 10, //  每页显示条数
        current_page: current,
        num_display_entries: 20, // 显示的页面个数
        next_text: '下一页',
        prev_text: '上一页',
        num_edge_entries: 2, //省略号后面显示的页码个数
        callback: function(index){
			if(vm.form.pageNum != (index + 1)){
				vm.form.pageNum = index + 1;
			    vm.search();
			}
        }
    });
}
